package com.zlkj.pro.modular.app.controller;

import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.zlkj.pro.core.util.JsonUtils;
import com.zlkj.pro.modular.admin.model.dto.SmsDTO;
import com.zlkj.pro.modular.admin.model.dto.UserDTO;
import com.zlkj.pro.modular.admin.model.vo.UserVO;
import com.zlkj.pro.modular.app.service.AppUsersService;
import com.zlkj.pro.modular.business.model.vo.AgentVO;
import com.zlkj.pro.modular.business.service.MessageService;
import com.zlkj.pro.modular.business.service.ReportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zlkj.pro.core.consts.ProjectConstants.SERVICE_APP_PREFIX;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiResource(name = "用户", path = SERVICE_APP_PREFIX + "/user")
public class AppUserController {

    private final AppUsersService appUsersService;

    private final ReportService reportService;

    private final MessageService messageService;


    @PostResource(name = "登录", path = "/login")
    public ResponseData login(@RequestBody UserDTO dto) {
        UserVO userVo = appUsersService.login(dto, true);
        return new SuccessResponseData(userVo);
    }

    @PostResource(name = "登录", path = "/loginWithSmsCode")
    public ResponseData loginWithSmsCode(@RequestBody UserDTO dto) {
        UserVO userVo = appUsersService.loginWithSmsCode(dto);
        return new SuccessResponseData(userVo);
    }


    @PostResource(name = "获取验证码", path = "/getSmsCode")
    public ResponseData getSmsCode(@RequestBody SmsDTO dto) {
        appUsersService.getSmsCode(dto);
        return new SuccessResponseData();
    }


    @PostResource(name = "忘记密码", path = "/forgotPassword")
    public ResponseData forgotPassword(@RequestBody UserDTO dto) {
        appUsersService.forgotPassword(dto);
        return new SuccessResponseData();
    }

    @PostResource(name = "修改密码", path = "/changePassword")
    public ResponseData changePassword(@RequestBody UserDTO dto) {
        appUsersService.changePassword(dto);
        return new SuccessResponseData();
    }

    @PostResource(name = "修改密码", path = "/changePhone")
    public ResponseData changePhone(@RequestBody UserDTO dto) {
        appUsersService.changePhone(dto);
        return new SuccessResponseData();
    }

    @PostResource(name = "修改姓名", path = "/changeName")
    public ResponseData changeName(@RequestBody UserDTO dto) {
        appUsersService.changeName(dto);
        return new SuccessResponseData();
    }

    @PostResource(name = "修改头像", path = "/changeAvatar")
    public ResponseData changeAvatar(@RequestBody UserDTO dto) {
        appUsersService.changeAvatar(dto);
        return new SuccessResponseData();
    }


    @GetResource(name = "个人信息", path = "/getUserInfo")
    public ResponseData getUserInfo(Long userId) {
        UserVO userVo = appUsersService.getUserInfo(userId);
        return new SuccessResponseData(userVo);
    }


    @GetResource(name = "代理商信息", path = "/getAgentInfo")
    public ResponseData getAgentInfo(Long userId) {
        AgentVO agentVO = appUsersService.getAgentInfo(userId);
        return new SuccessResponseData(agentVO);
    }

    @GetResource(name = "代理商员工列表", path = "/getAgentUserList")
    public ResponseData getAgentUserList(UserDTO dto) {
        List<UserVO> userVOs = appUsersService.getAgentUserList(dto);
        return new SuccessResponseData(userVOs);
    }

    @GetResource(name = "代理商经理列表", path = "/findManagers")
    public ResponseData findManagers(Long userId) {
        List<UserVO> userVOs = appUsersService.findManagers(userId);
        return new SuccessResponseData(userVOs);
    }

    @PostResource(name = "添加员工", path = "/add")
    public ResponseData add(@RequestBody UserDTO dto) {
        boolean restData = appUsersService.add(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "查询员工详情", path = "/detail")
    public ResponseData detail(UserDTO dto) {
        UserVO restData = appUsersService.detail(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "编辑员工信息", path = "/edit")
    public ResponseData edit(@RequestBody UserDTO dto) {
        boolean restData = appUsersService.edit(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "首页计数", path = "/count")
    public ResponseData count(UserDTO dto) {
        log.info("首页计数参数：" + JsonUtils.toJson(dto));
        Map<String, Object> map = new HashMap<>();
        map.put("reportCount", reportService.reportCount(dto.getUserId()));// 报备单计数
        map.put("messageCount", messageService.countUnread(dto.getUserId()));// 消息计数
        return new SuccessResponseData(map);
    }

}
